package com.aigc.sclassroom.mapper;

import com.aigc.sclassroom.pojo.ChatMessage;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ChatMessageMapper {

    /**
     * 保存聊天消息到数据库
     * @param chatMessage 聊天消息对象
     * @return 插入操作影响的行数
     */
    @Insert("INSERT INTO chatmessage (sender, receiver, message) " +
            "VALUES (#{sender}, #{receiver}, #{message})")
    @Options(useGeneratedKeys = true, keyProperty = "chatMessageId", keyColumn = "chat_message_id")
    int saveMessage(ChatMessage chatMessage);

    /**
     * 根据发送者和接收者查询聊天历史记录
     * @param sender 发送者用户名
     * @param receiver 接收者用户名
     * @param receiver2 接收者用户名（用于交换条件）
     * @param sender2 发送者用户名（用于交换条件）
     * @return 聊天历史记录列表
     */
    @Select("SELECT * FROM chatmessage " +
            "WHERE (sender = #{sender} AND receiver = #{receiver}) " +
            "OR (sender = #{receiver2} AND receiver = #{sender2}) " +
            "ORDER BY create_time ASC")
    List<ChatMessage> findMessage(String sender, String receiver, String receiver2, String sender2);


    @Select("SELECT * FROM chatmessage " +
            "WHERE (receiver = 'group') "+
            "ORDER BY create_time ASC")
    List<ChatMessage> findMessageById(String sender, String receiver);

}
